from .Query import Query


class QueryStudentsGrade(Query):
    """
    # 根据学号或学生姓名,查询学生个人成绩
    """
    __sql = """
    SELECT
        STUDENTNO,
        STUNAME,
        TERM,
        ACADEMICYEAR,
        COURSECODE,
        COURSENAME,
        ENDSCORE,
        FINALSCORE
    FROM
        t_courseinfo_students
    WHERE
        STUDENTNO = %s
    AND STUNAME = %s
    AND ACADEMICYEAR LIKE %s
    AND TERM LIKE %s
    """

    def __is_pass(self, is_pass):
        """检查is_pass标记,如果有就添加相应的SQL"""
        _fail = """
        AND
        (
            FINALSCORE IN ('不及格', '不通过')
            OR FINALSCORE IS NULL
            OR FINALSCORE < '60'
        )
        """
        _pass = """
        AND NOT
        (
            FINALSCORE IN ('不及格', '不通过')
            OR FINALSCORE IS NULL
		    OR FINALSCORE < '60'
        )
        """
        if is_pass is not None or len(is_pass) > 0:
            if "TRUE" == str(is_pass).upper():
                self.__sql += _pass
                pass
            elif "FALSE" == str(is_pass).upper():
                self.__sql += _fail
                pass

    def __init__(self, request):
        student_no = self.len_zero_to_default(request.get("student_no", "%"))
        student_name = self.len_zero_to_default(request.get("student_name", "%"))
        term = self.len_zero_to_default(request.get("term", "%"))
        academic_year = self.len_zero_to_default(request.get("academic_year", "%"))
        self.__is_pass(request.get("is_pass", ""))
        self.execute_sql(self.__sql,
                         student_no,
                         student_name,
                         academic_year,
                         term,
                         )
        pass

    pass
